package winTime.process;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import winTime.Start;
import winTime.vo.TickKLine;
import winTime.vo.tickData;
/**
 * @author fatsung
 * 處理邏輯,讀取queue中的Tick資料
 */
public class Worker extends Thread{
	private static final Logger log=LoggerFactory.getLogger(Worker.class);
	private static int totQty=0;//修正成交口數差異, 18:成交量, 19:總成交量
	@Override
	public void run() {
		log.trace("Worker start");
		while(true){
			try{
				doWorkerSecondKRPT((tickData)Start.poll());
			}catch(Exception e){}
		}
	}

	private void doWorkerSecondKRPT(tickData obj){
		TickKLine tk;
		TickKLine tk1 = new TickKLine();
		int ttime,price,qty,sumqty;
		ttime = obj.gettTime();
		price = obj.getPrice();
		qty = obj.getQty();
		sumqty = obj.getSumqty();
		tk1.setCtime(ttime);
//		log.trace("ttime:{}, second:{}",ttime,ttime/100);
		if(Start.td.contains(tk1)){
//			log.trace("log1");
			Start.td.get(Start.td.indexOf(tk1)).setData(ttime, price, qty);
		}else{
//			log.trace("log2:");
			tk = new TickKLine();
			tk.setData(ttime, price, qty);
			Start.td.add(tk);
		}
		
		
	}
	/**
	 * 計算秒K的Tick資料
	 * 資料置入 Start的MAP之中
	 * @param obj
	 */
	private void doWorkerSecondK(tickData obj){
		TickKLine tk;
		TickKLine tk1 = new TickKLine();
		int ttime,price,qty,sumqty;
		ttime = obj.gettTime();
		price = obj.getPrice();
		qty = obj.getQty();
		sumqty = obj.getSumqty();
		tk1.setCtime(ttime/100);
		if(qty+totQty<sumqty){
			qty=sumqty-totQty;
		}
		if(sumqty>totQty){
			totQty=sumqty;
		}else{
			log.debug("time:{}",ttime);
		}
//		log.trace("ttime:{}, second:{}",ttime,ttime/100);
		if(Start.td.contains(tk1)){
//			log.trace("log1");
			Start.td.get(Start.td.indexOf(tk1)).setData(ttime/100, price/1000, qty);
		}else{
//			log.trace("log2:");
			tk = new TickKLine();
			tk.setData(ttime/100, price/1000, qty);
			Start.td.add(tk);
		}
		
		
	}
}
